package com.example.live

import android.os.CountDownTimer
import android.view.View
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.mvi.mvi.BaseMviActivity
import com.bw.mvi.mvi.UiState
import com.example.base.app.RouterPath
import com.example.image.glide.GlideImgLoader
import com.example.live.databinding.ActivityLiveRoomBinding
import com.example.live.dialog.RedEnvelopeDialog
import com.example.live.entity.LiveEntityItem
import com.example.live.mvi.LiveViewModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat

@AndroidEntryPoint
@Route( path = RouterPath.LIVE_ROOM_ACTIVITY)
class LiveRoomActivity : BaseMviActivity<ActivityLiveRoomBinding, LiveViewModel>()  {
    @Autowired
    lateinit var video: LiveEntityItem
    lateinit var countDownTimer: CountDownTimer
    lateinit var exoPlayer: ExoPlayer
    var time = 0L
    override fun successData(it: UiState.onSuccess<*>) {

    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        // 1. 初始化 ExoPlayer
        exoPlayer = ExoPlayer.Builder(this).build()
        exoPlayer.repeatMode = Player.REPEAT_MODE_ALL
        // 2. 绑定 PlayerView
        binding.playerView.player = exoPlayer
        // 3. 设置媒体资源（URL 或本地文件）
        val mediaItem = MediaItem.fromUri(video.videopath) // 替换为你的视频URL
        exoPlayer.setMediaItem(mediaItem)
        exoPlayer.prepare()
        exoPlayer.play() // 自动播放
        binding.liveDetailAuthorName.text = video.name
        GlideImgLoader.loadCircleImage(this, video.avatar_url, binding.liveDetailAuthorIcon)

    }

    override fun initData() {
        //退出直播间
        binding.liveDetailClose.setOnClickListener {
            XPopup.Builder(this)
                .autoDismiss(false) // 点击按钮不自动关闭
                .hasShadowBg(true) // 显示半透明背景
                .asCustom(object : BasePopupView(this) {
                    override fun getInnerLayoutId(): Int = R.layout.exit_dialog_layout
                    override fun onCreate() {
                        super.onCreate()
                        findViewById<View>(R.id.exit).setOnClickListener {
                            finish()
                            dismiss()
                        }
                        findViewById<View>(R.id.find).setOnClickListener {
                            ToastUtils.showShort("寻找更多主播")
                        }
                    }
                })
                .show()

        }

        //红包倒计时
        countDownTimer = object : CountDownTimer(80000, 1000) {
            override fun onTick(p0: Long) {
                time = p0
                val simpleDateFormat = SimpleDateFormat("mm:ss")
                val s = simpleDateFormat.format(p0)
                binding.redEnvelopeTime.text = s
            }

            override fun onFinish() {
                binding.redEnvelopeTime.visibility = View.GONE
                binding.redEnvelope.visibility = View.GONE
            }
        }.start()

        //红包dialog
        binding.redEnvelope.setOnClickListener {
            val dialog = RedEnvelopeDialog(this)
            dialog.setTime(time)
            dialog.show()
        }
    }





}